﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace mediaPrime.Controls
{
    public class MPNumPad : Control
    {
        static MPNumPad()
        {
            // Use another default style than the base class
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
                typeof(MPNumPad), new FrameworkPropertyMetadata(typeof(MPNumPad))
            );
        }



        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(MPNumPad), new PropertyMetadata("0"));



        public MPNumPad()
        {
            this.Loaded += MPNumPad_Loaded;
        }

        private void MPNumPad_Loaded(object sender, RoutedEventArgs e)
        {
            var traversalRequest = new TraversalRequest(FocusNavigationDirection.Next);
            MoveFocus(traversalRequest);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            for (int i = 0; i < 9; i++)
            {
                var button = Template.FindName("PART_" + i.ToString(), this) as MPTileButton;
                if (button != null)
                    button.Click += button_Click;
            }

            var buttonClear = Template.FindName("PART_CLEAR", this) as MPTileButton;
            if (buttonClear != null)
                buttonClear.Click += buttonClear_Click;

            var buttonRemove = Template.FindName("PART_REMOVE", this) as MPTileButton;
            if (buttonRemove != null)
                buttonRemove.Click += buttonRemove_Click;
        }

        void buttonRemove_Click(object sender, RoutedEventArgs e)
        {
            var currentValue = GetValue(ValueProperty) as string;
            if (!string.IsNullOrEmpty(currentValue))
            {
                currentValue = currentValue.Substring(0, currentValue.Length - 1);
                SetValue(ValueProperty, currentValue);
            }
        }

        void buttonClear_Click(object sender, RoutedEventArgs e)
        {
            SetValue(ValueProperty, string.Empty);
        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as MPTileButton;
            if (button != null)
            {
                switch (button.Name)
                {
                    case "PART_1": SetValue(ValueProperty, GetValue(ValueProperty) as string + "1"); break;
                    case "PART_2": SetValue(ValueProperty, GetValue(ValueProperty) as string + "2"); break;
                    case "PART_3": SetValue(ValueProperty, GetValue(ValueProperty) as string + "3"); break;
                    case "PART_4": SetValue(ValueProperty, GetValue(ValueProperty) as string + "4"); break;
                    case "PART_5": SetValue(ValueProperty, GetValue(ValueProperty) as string + "5"); break;
                    case "PART_6": SetValue(ValueProperty, GetValue(ValueProperty) as string + "6"); break;
                    case "PART_7": SetValue(ValueProperty, GetValue(ValueProperty) as string + "7"); break;
                    case "PART_8": SetValue(ValueProperty, GetValue(ValueProperty) as string + "8"); break;
                    case "PART_9": SetValue(ValueProperty, GetValue(ValueProperty) as string + "9"); break;
                    case "PART_0": SetValue(ValueProperty, GetValue(ValueProperty) as string + "0"); break;
                }
            }
        }
    }
}
